<?php

namespace app\admin\controller;


class User extends BaseController {
    /**
     * gender 0:全部 1:男性 2:女性
     * order 0:按ID排序 1:按发布次数排序 2:按登录次数排序 3:按登录时间排序
     */
    public function index() {
        $query     = input('get.');
        $condition = [];
        $order     = [];
        if (!empty($query['gender'])) {
            $condition['gender'] = $query['gender'];
        }
        if (!empty($query['city'])) {
            $condition['city'] = $query['city'];
        }
        if (!empty($query['province'])) {
            $condition['province'] = $query['province'];
        }
        if (!empty($query['keyword'])) {
            $condition['nickName'] = ['like', '%' . $query['keyword'] . '%'];
        }
        if (!empty($query['order'])) {
            switch (intval($query['order'])) {
                case 1:
                    $order = ['id' => 'desc'];
                    break;
                case 2:
                    $order = ['publish_count' => 'desc'];
                    break;
                case 3:
                    $order = ['login_count' => 'desc'];
                    break;
                case 4:
                    $order = ['update_time' => 'desc'];
            }
        }
        $this->getPageAndSize($query);
        $result = model('User')->getUserList($condition, $this->from, $this->size, $order);
        return show(0, 'get userInfo success', $result);
    }

    public function getCityAndProvince() {
        $result = model('User')->getCityAndProvince();
        return show(0, 'ok', $result);
    }

    public function update($id) {
        $forbidden = input('put.forbidden', 1, 'intval') == 0 ? 1 : 0;
        $result    = model('User')->where(['id' => $id])->update(['forbidden' => $forbidden]);
        if (!$result) {
            return show(1, '修改失败');
        }
        return show(0, '修改成功');
    }
}